/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.jr2tecnologia.model;

import br.com.jr2tecnologia.LoggerFactory;
import br.com.jr2tecnologia.model.entity.BankAccount;
import br.com.jr2tecnologia.model.entity.ClientPhone;
import br.com.jr2tecnologia.model.entity.Company;
import br.com.jr2tecnologia.model.entity.Credit;
import br.com.jr2tecnologia.model.entity.KeyResponse;
import br.com.jr2tecnologia.model.entity.Login;
import br.com.jr2tecnologia.model.entity.SmsReceived;
import br.com.jr2tecnologia.model.entity.SmsSend;
import br.com.jr2tecnologia.model.persistence.DAOFactory;
import br.com.jr2tecnologia.model.repository.BankAccountRepository;
import br.com.jr2tecnologia.model.repository.ClientPhoneRepository;
import br.com.jr2tecnologia.model.repository.CompanyRepository;
import br.com.jr2tecnologia.model.repository.CreditRepository;
import br.com.jr2tecnologia.model.repository.KeyResponseRepository;
import br.com.jr2tecnologia.model.repository.LoginRepository;
import br.com.jr2tecnologia.model.repository.SmsReceivedRepository;
import br.com.jr2tecnologia.model.repository.SmsSendRepository;
import java.util.List;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author ronildo
 */

@Stateless
public class CompanyPersistence implements CompanyPersistenceLocal{

    private static final Logger logger = LoggerFactory.getLogger();

    @PersistenceContext
    private EntityManager em;
    private CompanyRepository companyRepository;
    private SmsReceivedRepository smsReceivedRepository;
    private SmsSendRepository smsSendRepository;
    private KeyResponseRepository keyResponseRepository;
    private ClientPhoneRepository clientPhoneRepository;
    private BankAccountRepository bankAccountRepository;
    private CreditRepository creditRepository;
    private LoginRepository loginRepository;

    @PostConstruct
    public void initialize(){
        companyRepository = DAOFactory.getCompanyRepository(em);
        smsReceivedRepository = DAOFactory.getSmsReceivedRepository(em);
        smsSendRepository = DAOFactory.getSmsSendRepository(em);
        keyResponseRepository = DAOFactory.getKeyResponseRepository(em);
        clientPhoneRepository = DAOFactory.getClientPhoneRepository(em);
        bankAccountRepository = DAOFactory.getBankAccountRepository(em);
        creditRepository = DAOFactory.getCreditRepository(em);
        loginRepository = DAOFactory.getLoginRepository(em);
    }

    public void persist(Company company){
        companyRepository.persist(company);
    }

    public void update(Company company){
        companyRepository.update(company);
    }

    public void remove(Company company){
        companyRepository.remove(company, company.getPkCompany());
    }

    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public void removeCascade(Company arg){
        Company company = companyRepository.findById(arg.getPkCompany());

        for(Login login : company.getLoginList()){
            if(login.getRole().getName().equals("root"))
                throw new PersistenceUnitException("Não é permitido remover esta conta");
            
            for(SmsReceived smsReceived : login.getSmsReceivedList())
                smsReceivedRepository.remove(smsReceived, smsReceived.getPkSmsReceived());
            for(SmsSend smsSend : login.getSmsSendList())
                smsSendRepository.remove(smsSend, smsSend.getPkSmsSend());
            for(KeyResponse keyResponse : login.getKeyResponseList())
                keyResponseRepository.remove(keyResponse, keyResponse.getPkKeyResponse());
            loginRepository.remove(login, login.getPkLogin());
        }

        for(ClientPhone clientPhone : company.getClientPhoneList())
            clientPhoneRepository.remove(clientPhone, clientPhone.getPkClientPhone());

        for(BankAccount bankAccount : company.getBankAccountList()){
            for(Credit credit : bankAccount.getCreditList())
                creditRepository.remove(credit, credit.getPkCredit());
            bankAccountRepository.remove(bankAccount, bankAccount.getPkBankAccount());
        }
        companyRepository.remove(company, company.getPkCompany());
    }

    public Company findById(Long id){
        return companyRepository.findById(id);
    }

    public Company findByName(String name){
        return companyRepository.findByName(name);
    }

    public List<Company> list(Company company){
        return companyRepository.list(company);
    }

    public List<Company> listAll(){
        return companyRepository.listAll();
    }
}
